package cn.good.yan.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置类
 * 接口文档地址：http://localhost:8080/doc.html
 *
 * @author shengren.yan
 * @create 2024/11/4
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {


    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 分组名称
                .groupName("2.X版本")
                .select()
                // 这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("cn.good.yan.model.controller"))
                .paths(PathSelectors.any())
                .build()
                // 经过授权，后通过Swagger接口访问， 不再提升
//        {
//            "code": 401,
//                "message": "尚未登陆，请重新登录！",
//                "obj": null
//        }
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("API 接口文档")
                .contact(new Contact("xxxyan", "http://www.xx.com/", "xxx邮箱"))
                .version("1.0")
                .build();
    }

    private List<ApiKey> securitySchemes() {
        // 设置请求头信息
        List<ApiKey> list = new ArrayList<>();
        // 第一个参数，这个当前的名字，key的名字
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "Header");
        list.add(apiKey);
        return list;
    }

    private List<SecurityContext> securityContexts() {
        // 设置需要登录认证的路径
        List<SecurityContext> list = new ArrayList<>();
        // hello下所有的接口
        list.add(getContextByPath("/hello/.*"));
        return list;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    // 默认的授权
    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> list = new ArrayList<>();
        // global 全局
        AuthorizationScope scope = new AuthorizationScope(
                "global", "mmm描述"
        );
        AuthorizationScope[] arr = new AuthorizationScope[1];
        arr[0] = scope;
        list.add(new SecurityReference("Authorization", arr));
        return list;
    }


}
